package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.ReservationPageDto;
import com.zzyl.nursing.vo.ReservationCountVo;
import com.zzyl.nursing.vo.ReservationPageVo;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约信息Service业务层处理
 *
 * @author alexis
 * @date 2025-06-03
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 新增预约信息
     * @param reservation
     */
    @Override
    public void saveReservation(Reservation reservation) {
        reservation.setStatus(0);
        reservationMapper.insertReservation(reservation);
    }

    /**
     * 查询今日取消预约次数
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public int getTodayCancelledCount(Long userId, LocalDateTime startTime, LocalDateTime endTime) {
        return reservationMapper.countTodayCancelledReservationsByUserIdAndTime(userId, startTime, endTime);
    }

    /**
     * 查询预约信息
     * @param time
     * @return
     */
@Override
public List<ReservationCountVo> getReservationCountByTime(Long time) {
    // 1. 转换时间戳为 LocalDateTime
    Instant instant = Instant.ofEpochMilli(time);
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
    // 3. 传递格式化后的字符串给 Mapper 方法
    List<ReservationCountVo> reservationCountVoList = reservationMapper.getReservationCountByTime(localDateTime);
    return reservationCountVoList;
}


    @Override
    public ReservationPageVo getReservationPage(ReservationPageDto reservationPageDto) {
        PageHelper.startPage(reservationPageDto.getPageNum(), reservationPageDto.getPageSize());
        List<Reservation> list =reservationMapper.getReservationPage(reservationPageDto);
        Page<Reservation> page = (Page<Reservation>) list;
        return new  ReservationPageVo(page.getTotal(),page.getResult());
    }

    @Override
    public int updateReservationById(Long id) {
        Reservation reservation = getById(id);
        reservation.setStatus(2);
        reservation.setUpdateBy(String.valueOf(UserThreadLocal.getUserId()));
        return updateById(reservation) ? 1 : 0;
    }

    @Override
    public void updateContractStatus() {
        List<Reservation> list = lambdaQuery().eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now())
                .list();
        if (Collections.isEmpty(list)){
            return;
        }
        list.forEach(reservation -> reservation.setStatus(3));
        updateBatchById(list);
    }
}
